using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content;

namespace XnaUtility
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class FPS : Microsoft.Xna.Framework.DrawableGameComponent
    {
        #region Properties
        private SpriteBatch sprBatch;
        private SpriteFont font;
        private Color fontColor;
        double refreshRate;
        private Vector2 location;

        long elapsedTime;
        int frameCount;
        int fps;
        string fpsMessage;

        
        public Vector2 Location
        {
            get { return location; }
            set { location = value; }
        }
	

        public double RefreshRate
        {
            get { return refreshRate; }
            set { refreshRate = value; }
        }

        public long RefreshTicks
        {
            get { return (long)((refreshRate / 1000) * TimeSpan.TicksPerSecond); }
        }

        public Color FontColor
        {
            get { return fontColor; }
            set { fontColor = value; }
        }


        public SpriteFont Font
        {
            get { return font; }
            set { font = value; }
        }


        public SpriteBatch SprBatch
        {
            get { return sprBatch; }
            set { sprBatch = value; }
        }

        #endregion

        #region "Constructors and Initializers"
        public FPS(Game game)
            : base(game)
        {
            RefreshRate = 1000;
            elapsedTime = 0;
            frameCount = 0;
            fps = 0;
            fpsMessage = "FPS:";
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
        }
        #endregion


        #region "Update and Draw"
        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {


            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            elapsedTime += gameTime.ElapsedGameTime.Ticks;
            frameCount++;

            //System.Diagnostics.Debug.WriteLine(gameTime.ElapsedGameTime.Ticks.ToString());

            if (elapsedTime >= RefreshTicks)
            {
                fps = (int)(frameCount / (elapsedTime / TimeSpan.TicksPerSecond));
                fpsMessage = "FPS: " + fps.ToString();
                elapsedTime = 0;
                frameCount = 0;
            }

            SprBatch.Begin();
            SprBatch.DrawString(Font, fpsMessage, location, FontColor);
            SprBatch.End();
            base.Draw(gameTime);
        }
        #endregion
    }
}
